package icu.zlz.emqx.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import icu.zlz.common.security.annotation.Logical;
import icu.zlz.emqx.domain.IotGroupUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import icu.zlz.common.log.annotation.Log;
import icu.zlz.common.log.enums.BusinessType;
import icu.zlz.common.security.annotation.RequiresPermissions;
import icu.zlz.emqx.domain.IotGroup;
import icu.zlz.emqx.service.IIotGroupService;
import icu.zlz.common.core.web.controller.BaseController;
import icu.zlz.common.core.web.domain.AjaxResult;
import icu.zlz.common.core.utils.poi.ExcelUtil;
import icu.zlz.common.core.web.page.TableDataInfo;

/**
 * 设备组Controller
 *
 * @author zangs
 * @since 2024-12-27
 */
@RestController
@RequestMapping("/group")
public class IotGroupController extends BaseController {
    @Autowired
    private IIotGroupService iotGroupService;

    /**
     * 查询设备组列表
     */
    @RequiresPermissions("emqx:group:list")
    @GetMapping("/list")
    public TableDataInfo list(IotGroup iotGroup) {
        startPage();
        List<IotGroup> list = iotGroupService.selectIotGroupList(iotGroup);
        return getDataTable(list);
    }



    /**
     * 导出设备组列表
     */
    @RequiresPermissions("emqx:group:export")
    @Log(title = "设备组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotGroup iotGroup) {
        List<IotGroup> list = iotGroupService.selectIotGroupList(iotGroup);
        ExcelUtil<IotGroup> util = new ExcelUtil<IotGroup>(IotGroup.class);
        util.exportExcel(response, list, "设备组数据");
    }

    /**
     * 获取设备组详细信息
     */
    @RequiresPermissions("emqx:group:query")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {
        return success(iotGroupService.selectIotGroupByGroupId(groupId));
    }

    /**
     * 新增设备组
     */
    @RequiresPermissions("emqx:group:add")
    @Log(title = "设备组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody IotGroup iotGroup) {
        return toAjax(iotGroupService.insertIotGroup(iotGroup));
    }

    /**
     * 修改设备组
     */
    @RequiresPermissions("emqx:group:edit")
    @Log(title = "设备组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotGroup iotGroup) {
        return toAjax(iotGroupService.updateIotGroup(iotGroup));
    }

    /**
     * 删除设备组
     */
    @RequiresPermissions("emqx:group:remove")
    @Log(title = "设备组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        return toAjax(iotGroupService.deleteIotGroupByGroupIds(groupIds));
    }


    /**
     * 查询设备组当前的用户列表
     */
    @RequiresPermissions(value = {"emqx:group:list", "emqx:group:edit", "emqx:group:query", "emqx:group:add"}, logical = Logical.OR)
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(IotGroupUser user) {

        startPage();
        List<Map<String, Object>> list = iotGroupService.selectAllocatedList(user);
        return getDataTable(list);
    }

    /**
     * 查询非设备组的用户列表（即可以分配的用户）
     */
    @RequiresPermissions(value = {"emqx:group:user_add"}, logical = Logical.OR)
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(IotGroupUser user) {
        startPage();
        List<Map<String, Object>> list = iotGroupService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 批量分配用户
     */
    @RequiresPermissions("emqx:group:user_add")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(Long groupId, Long[] userIds, String[] permissions) {
        return toAjax(iotGroupService.insertGroupUsers(groupId, userIds, permissions));
    }

    /**
     * 取消授权用户
     */
    @RequiresPermissions("emqx:group:user_remove")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody IotGroupUser iotGroupUser) {

        return toAjax(iotGroupService.deleteAuthUser(iotGroupUser));
    }

    /**
     * 批量取消授权用户
     */
    @RequiresPermissions("emqx:group:user_remove")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(Long groupId, Long[] userIds) {
        return toAjax(iotGroupService.deleteAuthUsers(groupId, userIds));
    }

    /**
     * 查询用户权限
     */
    @Log(title = "设备组", businessType = BusinessType.EXPORT)
    @GetMapping("/MyPermissions/{groupId}")
    public AjaxResult export(@PathVariable  Long groupId) {
      return  AjaxResult.success(iotGroupService.getMyPermissions(groupId));
    }


}
